package club.justwill.single.Controller;

import club.justwill.single.Service.ActivityService;
import club.justwill.single.aspect.LoginRequired;
import club.justwill.single.assembler.ActivityAssemble;
import club.justwill.single.bean.Activity.ActivityResp;
import club.justwill.single.bean.Activity.CreateActivity;
import club.justwill.single.bean.Activity.GetActivityResp;
import club.justwill.single.bean.Activity.JoinActivity;
import club.justwill.single.bean.SearchActivity;
import club.justwill.single.bean.SearchProjects;
import club.justwill.single.model.CoreConstants;
import club.justwill.single.model.JsonResponse;
import club.justwill.single.model.SecurityToken;
import club.justwill.single.persistence.jpo.Activity;
import club.justwill.single.util.JsonResponseHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.WebApplicationContext;

import java.util.List;
import java.util.Map;

/**
 * Created by xiangnan on 16-1-15.
 */
@RestController
@Scope(WebApplicationContext.SCOPE_SESSION)
public class ActivityController extends BaseController {

    @Autowired
    private ActivityService activityService;

    @Autowired
    private ActivityAssemble activityAssemble;

    @LoginRequired
    @RequestMapping(value = URL_JOIN_ACTIVITY, method = RequestMethod.POST, produces = CoreConstants.MIME_JSON)
    public JsonResponse joinActivity(@PathVariable Long activityId) {
        JsonResponse obj = new JsonResponse();
        SecurityToken token = new SecurityToken(sessionState.getUserId());
        token.generateToken(sessionState);
        activityService.joinActivity(token, obj, activityId);
        if(JsonResponseHelper.isHasError(obj)) {
            obj.setFailed();
        } else {
            obj.setSuccessful();
        }
        return obj;
    }
    @LoginRequired
    @RequestMapping(value = URL_CREATE_ACTIVITY, method = RequestMethod.POST, produces = CoreConstants.MIME_JSON)
    public JsonResponse createActivity(@RequestBody CreateActivity createActivity) {
        JsonResponse obj = new JsonResponse();
        SecurityToken token = new SecurityToken(sessionState.getUserId());
        token.generateToken(sessionState);
        Activity activity = activityService.createActivity(token, obj, createActivity);
        if(JsonResponseHelper.isHasError(obj)) {
            obj.setFailed();
        } else {
            obj.setData(activity);
            obj.setSuccessful();
        }
        return obj;
    }
    @LoginRequired
    @RequestMapping(value = URL_GET_ACTIVITY, method = RequestMethod.POST, produces = CoreConstants.MIME_JSON)
    public JsonResponse getUserActivity() {
        JsonResponse obj = new JsonResponse();
        SecurityToken token = new SecurityToken(sessionState.getUserId());
        token.generateToken(sessionState);
        Map<String, List> activityMap = activityService.getActivity(token, obj);
        if(JsonResponseHelper.isHasError(obj)) {
            obj.setFailed();
        } else {
            obj.setSuccessful();
            GetActivityResp getActivityResp = new GetActivityResp();
            getActivityResp.setCreate(activityAssemble.assembleActivityList(activityMap.get("create")));
            getActivityResp.setJoin(activityAssemble.assembleActivityList(activityMap.get("join")));
            obj.setData(getActivityResp);
        }
        return obj;
    }

    @RequestMapping(value = URL_SEARCH_ACTIVITY, method = RequestMethod.POST, produces = CoreConstants.MIME_JSON)
    public JsonResponse searchActivity(@RequestBody SearchActivity searchActivity) {
        JsonResponse obj = new JsonResponse();
        List<Activity> activityList = activityService.searchActivity(obj, searchActivity);
        List<ActivityResp> activityRespList = activityAssemble.assembleActivityList(activityList);
        if (JsonResponseHelper.isHasError(obj)) {
            obj.setFailed();
        } else {
            obj.setData(activityRespList);
            obj.setSuccessful();
        }
        return obj;
    }
}
